package system
import(
	"os"
	"path/filepath"
	"os/user"
	"fmt"
)
type OS struct{
}
//获取主机名
func (this *OS) GetHostName() string{
	name,_:=os.Hostname()
		return name
}
//获取指定文件的信息，如果是目录的话，读取子文件信息
func (this *OS) GetFileInfo(v string) *SystemFile{
	if v==""{return nil}
	if !filepath.IsAbs(v){
		fmt.Println(v)
		file:=SystemFile{}
		file.Error="Not absolute path"
		return &file
	}
	v=filepath.FromSlash(v)
	info,err:=os.Stat(v)
	if err!=nil{
		file:=SystemFile{}
		file.Error=err.String()
		return &file
	}
	file:=transformFileInfo(info)
	file.Path=v
	if file.IsFolder{
		children,err:=getFolderChildren(v)
		file.Children=children
		if err!=nil{
			file.Error=err.String()
		}
	}
	return file
}
//将os.FileInfo转换为SystemFile
func transformFileInfo(info *os.FileInfo) *SystemFile{
	file:=SystemFile{}
	file.IsFolder=info.IsDirectory()
	file.IsChar=info.IsChar()
	file.IsRegular=info.IsRegular()
	file.Name=info.Name
	file.Permission=info.Permission()
	file.Size=info.Size
	if file.IsFolder==false{
		file.MimeType=GetContentType(file.Name)
	}
	return &file
}
//获取指定目录下子文件的信息
func getFolderChildren(s string) ([]SystemFile,os.Error){
	f,err:=os.Open(s)
	if err!=nil{return nil,err}
	infos,err:=f.Readdir(0)
	var files []SystemFile
	for _,info:= range infos{
		file:=*transformFileInfo(&info)
		file.Path=filepath.Join(s,file.Name)
		files=append(files,file)
	}
	return files,err
}
func GetFileRoot(){

}
func (this *OS) GetUser() (*user.User,os.Error){
	return user.LookupId(os.Getuid())
}
